{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The below derives from\n",
    "https://github.com/yihui/knitr/blob/master/inst/examples/knitr-spin.R\n",
    "\n",
    "This is a special R script which can be used to generate a report. You can\n",
    "write normal text in roxygen comments.\n",
    "\n",
    "First we set up some options (you do not have to do this):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hide_input": true,
    "hide_output": true,
    "name": "setup"
   },
   "outputs": [],
   "source": [
    "library(knitr)\n",
    "opts_chunk$set(fig.path = 'figure/silk-')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The report begins here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cache": false,
    "name": "test-a"
   },
   "outputs": [],
   "source": [
    "# boring examples as usual\n",
    "set.seed(123)\n",
    "x = rnorm(5)\n",
    "mean(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lines_to_next_cell": 0
   },
   "source": [
    "You can not use here the special syntax {{code}} to embed inline expressions, e.g."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "lines_to_next_cell": 0
   },
   "outputs": [],
   "source": [
    "{{mean(x) + 2}}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "is the mean of x plus 2.\n",
    "The code itself may contain braces, but these are not checked.  Thus,\n",
    "perfectly valid (though very strange) R code such as `{{2 + 3}} - {{4 - 5}}`\n",
    "can lead to errors because `2 + 3}} - {{4 - 5` will be treated as inline code.\n",
    "\n",
    "Now we continue writing the report. We can draw plots as well."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "fig.height": 5,
    "fig.width": 5,
    "name": "test-b"
   },
   "outputs": [],
   "source": [
    "par(mar = c(4, 4, .1, .1)); plot(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Actually you do not have to write chunk options, in which case knitr will use\n",
    "default options. For example, the code below has no options attached:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "var(x)\n",
    "quantile(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And you can also write two chunks successively like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "lines_to_next_cell": 0,
    "name": "test-chisq5"
   },
   "outputs": [],
   "source": [
    "sum(x^2) # chi-square distribution with df 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "name": "test-chisq4"
   },
   "outputs": [],
   "source": [
    "sum((x - mean(x))^2) # df is 4 now"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Done. Call spin('knitr-spin.R') to make silk from sow's ear now and knit a\n",
    "lovely purse."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# /* you can write comments between /* and */ like C comments (the preceding #\n",
    "# is optional)\n",
    "Sys.sleep(60)\n",
    "# */"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# /* there is no inline comment; you have to write block comments */"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "name,hide_input,fig.width,fig.height,cache,hide_output,-all",
   "main_language": "R",
   "notebook_metadata_filter": "-all"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
